model model3
/*
 * this model add ability of movement of vehicule agent 
 * chay van thay lung tung, khong theo do thi, mac du cuoi cung van den noi
 */
global {
	file shape_file_lignes <- file('../includes/lignes.shp');
	file shape_file_enveloppes <- file('../includes/enveloppes.shp');
	file shape_file_terrains <- file('../includes/terrains.shp');
	file shape_file_departs <- file('../includes/departs.shp');
	file shape_file_arrivees <- file('../includes/arrivees.shp');
	file shape_file_feuxsignaux <- file('../includes/feuxsignaux.shp');
	int nb_vehicule <- 50;
	float min_speed <- 10.0;
	float max_speed <- 20.0;
	graph the_graph;
	
	
	init {
		create ligne from: shape_file_lignes;
		set the_graph <- as_edge_graph(list (ligne) ) with_optimizer_type "Dijkstra";
//		set graph_path <- as_edge_graph ( list ( path_emergent ) )with_optimizer_type "Dijkstra" 
		
		create terrain from: shape_file_terrains;
		create depart from: shape_file_departs;
		create arrivee from: shape_file_arrivees;
		create feusignal from: shape_file_feuxsignaux;
		let list_departs type:list of:depart <- list(depart);
		let list_arrivees type:list of:arrivee <- list(arrivee);
//		let common_arrivee type:arrivee <- one_of (list_arrivees);
		create vehicule number: nb_vehicule {
			set speed <- min_speed + rnd(max_speed - min_speed);
			set the_depart <- one_of (list_departs);
			set the_arrivee <- one_of (list_arrivees);
//			set the_arrivee <- common_arrivee;
			set location <- any_location_in( the_depart);
			set the_target <- any_location_in( the_arrivee);
		}
	}
}
entities {
	
	species ligne  {
		rgb color <- rgb('red') ;
		aspect base {
			draw geometry: shape color: color ;
		}
	}
	
	species terrain {
		string type;
		rgb color <- rgb('gray');
		aspect base {
			draw geometry: shape color: color;
		}
	}
	
	species depart {
		rgb color <- rgb('green');
		aspect base {
			draw geometry: shape color: color;
		}
	}
	
	species arrivee {
		rgb color <- rgb('blue');
		aspect base {
			draw geometry: shape color: color;
		}
	}
	
	species feusignal {
		rgb color <- rgb('black');
		aspect base {
			draw geometry: shape color: color;
		}
	}
	
	species vehicule skills:[moving]{
		rgb color <- rgb('yellow');
		depart the_depart <- nil;
		arrivee the_arrivee <- nil;
		point the_target <- nil;
		aspect base {
			draw shape: circle color: color size: 10;
		}
		
		reflex move when: the_target != nil {
			do goto target: the_target on: the_graph;
			switch the_target {
				match location {set the_target <- nil;}
			}
		}
	}
}

environment bounds: shape_file_enveloppes ;
	
experiment model1 type: gui {
	parameter 'Shapefile for the lignes:' var: shape_file_lignes category: 'GIS' ;
	parameter 'Shapefile for the terrains:' var: shape_file_terrains category: 'GIS' ;
	parameter 'Shapefile for the departs:' var: shape_file_departs category: 'GIS';
	parameter 'Shapefile for the arrivees:' var: shape_file_arrivees category: 'GIS';
	parameter 'Shapefile for the feux:' var: shape_file_feuxsignaux category: 'GIS';
	
	parameter 'Number of vehicule' var: nb_vehicule category:'Vehicule';
	output {
		display carrefour_display refresh_every: 1 {
			species ligne aspect: base ;
			species terrain aspect: base;
			species depart aspect: base;
			species arrivee aspect: base;
			species feusignal aspect: base;
			species vehicule aspect: base;
		}
	}
}